package yxy.cherry.data;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import yxy.apple.util.FileUtility;
import yxy.apple.util.JSONUtility;

public class DataMakerFunction {

	static String packageName = "yxy.cherry.data.function";

	public void make() {
		try {
			File[] files = FileUtility.getFiles(System.getProperty("user.dir") + "/data/json/", "function.json");
			for (File file : files) {
				makeFile(file, DataMaker.DIR, packageName);
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	void makeFile(File file, String dir, String packageName) throws Exception {
		// bean
		{
			String path = dir + "/" + packageName.replaceAll("\\.", "/");
			File output = new File(path + "/Function.java");
			if (output.exists()) {
				output.delete();
			}
			FileUtility.create(output);

//			System.err.println("\r\nmakefile::" + filename);
			String content = assembleContent(packageName, "Function", file);
//			System.err.println(content);
			FileOutputStream fos = new FileOutputStream(output);
			fos.write(content.getBytes("UTF-8"));
			fos.flush();
			fos.close();

			DataClassCheck.getInstance().inputFile(output);

			System.err.println("文件生成::" + output.getName());

		}
	}

	private String assembleContent(String packageName, String filename, File file) throws IOException {
		StringBuffer result = new StringBuffer();
		result.append(assembleHead(packageName, filename, file));
		result.append(assembleBody(packageName, filename, file));
		result.append("}");
		return result.toString();
	}

	private String assembleHead(String packageName, String filename, File file) {
		StringBuffer buffer = new StringBuffer();

		buffer.append("package " + packageName + ";\r\n\r\n");
		buffer.append("public enum Function {");
		buffer.append("\r\n");
		return buffer.toString();
	}

	private String assembleBody(String packageName, String filename, File file) throws IOException {
		StringBuffer bufferField = new StringBuffer();
		StringBuffer bufferEnum = new StringBuffer();

		JSONArray dataArray = JSONUtility.readArray(file);

		for (int i = 0; i < dataArray.size(); i++) {
			JSONObject jobj = dataArray.getJSONObject(i);
			int id = jobj.getIntValue("id");
			String name = jobj.getString("name");
			bufferEnum.append("	");
			bufferEnum.append(name);
			bufferEnum.append("(");
			bufferEnum.append(id);
			bufferEnum.append("),");
			bufferEnum.append("\r\n");
		}

		bufferField.append(bufferEnum.toString());

		bufferField.append("	;\r\n");
		bufferField.append("\r\n");
		bufferField.append("	private int id;\r\n");
		bufferField.append("\r\n");
		bufferField.append("	private Function(int id) {\r\n");
		bufferField.append("		this.id = id;\r\n");
		bufferField.append("	}\r\n");
		bufferField.append("\r\n");
		bufferField.append("	public int getId() {\r\n");
		bufferField.append("		return id;\r\n");
		bufferField.append("	}\r\n");

		return bufferField.toString();
	}

}
